package factory;

import privilege.Garnish;
import privilege.Privilege;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Properties;
import java.util.Set;

/**
 * @author lishunlin110
 * @version 1.0
 * @description: 特权类的工厂,创建类并输出结果
 * @date 2021/9/19 14:33
 */
public class GarnishFactory {
    private static final HashMap<String, Garnish> MAP = new HashMap<>();
    static {

        Properties p = new Properties();
        InputStream is = GarnishFactory.class.getClassLoader().getResourceAsStream("bean.properties");
        try {
            p.load(is);
            Set<Object> keys = p.keySet();
            for(Object key:keys){
                String className = p.getProperty((String) key);
                Class<?> createClass = Class.forName(className);
                Garnish privilege = (Garnish) createClass.newInstance();
                MAP.put((String) key,privilege);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static Privilege createGarnish(String name, Privilege privilege){
        if(name.isEmpty()){return privilege; }
        Garnish p = MAP.get(name);
        p.setPrivilege(privilege);
        return p;
    }
}
